/*
	FileName : JT_C_REC_Item.c
	Description :
		Manage the format of JT_C STS record item.
		Manage the buffer that saving the received STS items, 
		and JT_C working status according that.
	
	Created on 2017.11.02 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/

#include "JT_C_REC_Item.h"
#include "MiscFnx.h"		/// for CRC function
#include "JT_C_TAX.h"
#include "GF_WFL_PI.h"
//------------------------------------

JT_C_STATUS JTC_sts;

//-----------------------------------------------------------------------

uint8_t JT_C_REC_ITEM_isNoCode(JT_C_STATUS *jtcsts)
{
	if (jtcsts->currSD == JT_C_REC_ITEM_nocode_SD && 
		(jtcsts->currLed == LED_H || jtcsts->currLed == LED_B)
	)
		return 1;
	return 0;
}
uint8_t JT_C_REC_ITEM_isZJBERR(uint8_t *itemStartAddr)
{
	if(Mem_8b_Hbits(itemStartAddr,JT_C_REC_ITEM_BOARDVID, JT_C_REC_ITEM_BOARDVID_b_JMA1))
		return 1;
	if(Mem_8b_Hbits(itemStartAddr,JT_C_REC_ITEM_BOARDVID, JT_C_REC_ITEM_BOARDVID_b_JMA2))
		return 1;
	if(Mem_8b_Hbits(itemStartAddr,JT_C_REC_ITEM_BOARDVID, JT_C_REC_ITEM_BOARDVID_b_JMB1))
		return 1;
	if(Mem_8b_Hbits(itemStartAddr,JT_C_REC_ITEM_BOARDVID, JT_C_REC_ITEM_BOARDVID_b_JMB2))
		return 1;
	return 0;
}
uint8_t JT_C_REC_ITEM_JYJChanged(uint8_t *itemStartAddr)
{
	if (itemStartAddr[JT_C_REC_ITEM_JYJ] > JT_C_REC_ITEM_SAMPLE_50V_HIGH)
		JTC_sts.JYJ = 1;
	else
		JTC_sts.JYJ = 0;
	if (JTC_sts.JYJ == JTC_sts.lastJYJ){
		
		if(JTC_sts.JYJSameCnt != 0xFF)
			JTC_sts.JYJSameCnt++;
		if (JTC_sts.JYJSameCnt >= 2)
			JTC_sts.JYJChanged = 0;
	}
	else{
		
		JTC_sts.JYJSameCnt = 0;
		JTC_sts.JYJChanged = 1;
	}

	if(JTC_sts.JYJChanged)
		return 1;
	return 0;
}
//---------------------------------------------------------------------------------

#if (STS_FRAME_USE_EXTERNAL_MEM==0)
__ALIGN_BEGIN		STS_FRAME StsFrame[STS_BUF_SIZE];		__ALIGN_END;
#endif


void STS_FRAME_copyIn(STS_FRAME *sts_f, uint8_t *itemStartAddr)
{
	uint16_t i;
	uint8_t *ptr;
	ptr = (uint8_t *)(sts_f);
	for (i = 0; i < STS_FRAME_SIZE; i++)
		ptr[i] = itemStartAddr[i];
	return;
}
void STS_FRAME_recPartCopyOut(STS_FRAME *sts_f, uint8_t *readBuf)
{
	uint16_t i;
	uint8_t *ptr;
	ptr = (uint8_t *)sts_f;
	
	for (i = 0; i < STATUS_DATA_COUNT; i++)
		readBuf[i] = ptr[i];
	return;
}

void STS_FRAME_readNextUnhandledFrame(STS_FRAME *sts_f_array, 
										uint8_t *readBuf, 
										JT_C_STATUS *jtcsts,
										uint8_t frameNumber)
{
	uint16_t k,j,i;
	
	for (j = 0; j < frameNumber; j++){
		
		if (jtcsts->stsHandledCnt >= jtcsts->stsReceiveCnt){
			
			for (i = 0; i < STATUS_DATA_COUNT; i++)
				*readBuf++ = 0;
		}
		else{
			
			k = jtcsts->stsHandledCnt & STS_BUF_MASK;
			STS_FRAME_recPartCopyOut(&(sts_f_array[k]),readBuf);
			readBuf += STATUS_DATA_COUNT;
			jtcsts->stsHandledCnt++;
		}
	}
	return;
}

void STS_FRAME_setRecTypeAndReCrc(STS_FRAME *sts_f, uint8_t recType)
{
	uint8_t *ptr;
	
	ptr = (uint8_t*)(sts_f);
	
	Mem_LE_Set8(ptr,JT_C_REC_ITEM_RECTYPE,recType);
	STS_FRAME_reCrc(sts_f);
	
	return;
}

void STS_FRAME_reCrc(STS_FRAME *sts_f)
{
	uint16_t crc;
	uint8_t *ptr;
	
	ptr = (uint8_t*)(sts_f);
	
	crc = Calc_CRCByte(ptr, STATUS_DATA_COUNT - 2);
	Mem_LE_Set16(ptr,JT_C_REC_ITEM_CRC_L,crc);
	return;
}

//----------------------------------------------------------------------------------
void JT_C_STATUS_init(JT_C_STATUS *jtc)
{
	jtc->stsTime = 0;
	jtc->lastStsTime = 0xFF;
	jtc->lastOrder = 0xFF;
	
	jtc->stsReceiveCnt = 0;
	jtc->stsHandledCnt = 0;
	jtc->isFirstSts = 1;
	
	jtc->currLed = 0;
	jtc->lastLed = 0xFF;
	jtc->sameLedCnt = 0;
	jtc->noCodeCnt = 0;
	jtc->currSD = 0;
	jtc->lastSD = 0xFF;
	jtc->ledChangeCntInSameJYJ = 0;
	jtc->HomeStatus = 0;		//out home
	
	jtc->ledNoCode = 0;
	jtc->lastLedNoCode = 0;
	jtc->JYJChanged = 0;
	jtc->JYJSameCnt = 0;
	jtc->JYJ = 0;
	jtc->lastJYJ = 0;
	jtc->JCH_NO = 0;
	jtc->JC_type = 0;
	jtc->coilStatus = 0;
	jtc->SXXSilenceCnt = 0;
	return;
}

// if is the frame in the same second, return 1. not saving this frame to SD.20180428
uint8_t JT_C_STATUS_updateAllLastStatus(JT_C_STATUS *jtc, uint8_t *itemStartAddr)
{
	jtc->lastLed = jtc->currLed;
	jtc->lastSD = jtc->currSD;	
	jtc->lastOrder = Mem_LE_Get8(itemStartAddr,JT_C_REC_ITEM_ORDER);	
	jtc->lastLedNoCode = jtc->ledNoCode;
	jtc->lastJYJ = jtc->JYJ;
	if(jtc->lastStsTime == jtc->stsTime)
		return 1;
	else
		jtc->lastStsTime = jtc->stsTime;
	return 0;
}







///EOF
